package com.example.springboot.WebService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.springboot.dao.*;
import com.example.springboot.entity.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Random;

import static com.example.springboot.config.MD5.md5;

@RestController
@Api(tags = "WebFileService ",description = "以下是WebFileService类提供的接口")
@RequestMapping("/file")
public class WebFileService {
    @Autowired
    private BackgroundadminDao backgroundadminDao;
    @Autowired
    private BackgroundteacherDao backgroundteacherDao;
    @Autowired
    private CoursedataDao coursedataDao;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private CourseDao courseDao;
    @RequestMapping(value = "/uploadAdminAvatar", method = RequestMethod.POST)
    @CrossOrigin(origins = "*")
    @ApiOperation(value = "管理员上传头像", notes = "管理员上传头像到服务器")
    @ApiResponses({ @ApiResponse(code = 200, message = "上传成功"), @ApiResponse(code = 400, message = "参数错误"), @ApiResponse(code = 500, message = "服务器内部错误") })
    public String updateAdminAvatar(@RequestParam("file") MultipartFile avatar, @RequestParam("username") String username) throws IOException {
        if(avatar.isEmpty() || StringUtils.isBlank(username)) {
            return "参数错误";
        }else {
            String fileName = avatar.getOriginalFilename();
            System.out.println(fileName);
            String savePath = System.getProperty("user.dir") + "/src/main/resources/static/admin_sculpture/" + username+"_"+fileName;
            avatar.transferTo(new File(savePath));
            String filename_max = username+"_"+fileName;
            LambdaQueryWrapper<Backgroundadmin> backgroundadminLambdaQueryWrapper=new LambdaQueryWrapper<>();
            backgroundadminLambdaQueryWrapper.eq(Backgroundadmin::getAdmin,md5(username));
            int admin=backgroundadminDao.selectList(backgroundadminLambdaQueryWrapper).get(0).getId();
            Backgroundadmin backgroundadmin=new Backgroundadmin();
            backgroundadmin.setId(admin);
            backgroundadmin.setImage("http://127.0.0.1/admin_sculpture/"+filename_max);
            backgroundadminDao.updateById(backgroundadmin);
            return "管理员头像上传成功";
        }
    }
    @RequestMapping(value = "/uploadStudentAvatar", method = RequestMethod.POST)
    @CrossOrigin(origins = "*")
    @ApiOperation(value = "学生上传头像", notes = "学生上传头像到服务器")
    @ApiResponses({ @ApiResponse(code = 200, message = "上传成功"), @ApiResponse(code = 400, message = "参数错误"),
            @ApiResponse(code = 500, message = "服务器内部错误") })
    public String updateStudentAvatar(@RequestParam("file") MultipartFile avatar, @RequestParam("username") String username) throws IOException {
        if(avatar.isEmpty() || StringUtils.isBlank(username)) {
            return "参数错误";
        }else {
            String fileName = avatar.getOriginalFilename();
            String savePath = System.getProperty("user.dir") + "/src/main/resources/static/student_sculpture/" + username+"_"+fileName;
            avatar.transferTo(new File(savePath));
            String filename_max = username+"_"+fileName;
            LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
            userInfoLambdaQueryWrapper.eq(UserInfo::getUsername,username);
            int studentId=userInfoDao.selectList(userInfoLambdaQueryWrapper).get(0).getId();
            UserInfo userInfo=new UserInfo();
            userInfo.setId(studentId);
            userInfo.setSculpture("http://127.0.0.1/student_sculpture/"+filename_max);
            userInfoDao.updateById(userInfo);
            return "学生头像上传成功";
        }
    }
    @RequestMapping(value = "/uploadTeacherAvatar", method = RequestMethod.POST)
    @CrossOrigin(origins = "*")
    @ApiOperation(value = "老师上传头像", notes = "老师上传头像到服务器")
    @ApiResponses({ @ApiResponse(code = 200, message = "上传成功"), @ApiResponse(code = 400, message = "参数错误"),
            @ApiResponse(code = 500, message = "服务器内部错误") })
    public String updateTeacherAvatar(@RequestParam("file") MultipartFile avatar, @RequestParam("username") String username) throws IOException {
        if(avatar.isEmpty() || StringUtils.isBlank(username)) {
            return "参数错误";
        }else {
            String fileName = avatar.getOriginalFilename();
            String savePath = System.getProperty("user.dir") + "/src/main/resources/static/teacher_sculpture/" + username+"_"+fileName;
            avatar.transferTo(new File(savePath));
            String filename_max = username+"_"+fileName;
            LambdaQueryWrapper<Backgroundteacher> backgroundteacherLambdaQueryWrapper=new LambdaQueryWrapper<>();
            System.out.println(md5(username));
            backgroundteacherLambdaQueryWrapper.eq(Backgroundteacher::getTeacher,md5(username));
            int teacherId=backgroundteacherDao.selectList(backgroundteacherLambdaQueryWrapper).get(0).getId();
            Backgroundteacher backgroundteacher=new Backgroundteacher();
            backgroundteacher.setId(teacherId);
            backgroundteacher.setImage("http://127.0.0.1/teacher_sculpture/"+filename_max);
            backgroundteacherDao.updateById(backgroundteacher);
            System.out.println("老师头像上传成功，名为"+filename_max);
            return "老师头像上传成功";
        }
    }
    @RequestMapping(value = "/uploadVideo", method = RequestMethod.POST)
    @CrossOrigin(origins = "*")
    @ApiOperation(value = "上传视频", notes = "上传视频到服务器")
    @ApiResponses({ @ApiResponse(code = 200, message = "上传成功"), @ApiResponse(code = 400, message = "参数错误"),
            @ApiResponse(code = 500, message = "服务器内部错误") })
    public int uploadVideo(@RequestParam("file") MultipartFile video,@RequestParam("username") String username) throws IOException {
        if(video.isEmpty()) {
            return 0;
        }else {
            // 获取上传的文件名
            String fileName = video.getOriginalFilename();
            Random random=new Random();
            // 将文件保存到服务器
            String savePath = System.getProperty("user.dir") + "/src/main/resources/static/video/" + fileName;
            video.transferTo(new File(savePath));
            LambdaQueryWrapper<Course> courseLambdaQueryWrapper=new LambdaQueryWrapper<>();
            courseLambdaQueryWrapper.eq(Course::getCourseName,username);
            int courseId=courseDao.selectList(courseLambdaQueryWrapper).get(0).getId();
            Coursedata coursedata=new Coursedata();
            coursedata.setCoursename(username+"_"+fileName);
            coursedata.setCourseurl("http://127.0.0.1/video/"+fileName);
            coursedata.setCourseduration(String.valueOf(random.nextDouble()));
            coursedata.setCourseid(courseId);
            coursedataDao.insert(coursedata);
//            coursedata.setCourseurl("http://127.0.0.1/video/"+fileName);
            // 处理上传成功后的逻辑
            System.out.println("视频上传成功");
            return 1;
        }
    }
    

}
